<?php

$langage= recuperer_variable("lang", "");
/****************************************************************************************/
/*                                definition pagination                                 */
/****************************************************************************************/
$messagesParPage=10; //5 messages par page.

if(isset($_GET['lang'])) // Si la variable $_GET['page'] existe...
{
	$retour_total=mysql_query('SELECT COUNT(*) AS total FROM questions WHERE id_langage ='.$langage); //Nous récupérons le contenu de la requête dans $retour_total
}
else
{
	$retour_total=mysql_query('SELECT COUNT(*) AS total FROM questions');
}

$retour_total=mysql_query('SELECT COUNT(*) AS total FROM questions');
$donnees_total=mysql_fetch_assoc($retour_total);
$total=$donnees_total['total'];


$nombreDePages=ceil($total/$messagesParPage);//nombre de pages

if(isset($_GET['page'])) // Si la variable $_GET['page'] existe...
{
	$pageActuelle=intval($_GET['page']);

	if($pageActuelle>$nombreDePages) // Si la valeur de $pageActuelle (le numéro de la page) est plus grande que $nombreDePages...
	{
		$pageActuelle=$nombreDePages;
	}
}
else // Sinon
{
	$pageActuelle=1; // La page actuelle est la n°1 
}

$premiereEntree=($pageActuelle-1)*$messagesParPage; // On calcul la première entrée à lire

?>


<!--  start page-heading -->
<div id="page-heading">
<h1>Vos dernières questions</h1>
</div>
<!-- end page-heading -->

<table border="0" width="100%" cellpadding="0" cellspacing="0"
	id="content-table">
	<tr>
		<th rowspan="3" class="sized"><img
			src="images/shared/side_shadowleft.jpg" width="20" height="300"
			alt="" /></th>
		<th class="topleft"></th>
		<td id="tbl-border-top">&nbsp;</td>
		<th class="topright"></th>
		<th rowspan="3" class="sized"><img
			src="images/shared/side_shadowright.jpg" width="20" height="300"
			alt="" /></th>
	</tr>
	<tr>
		<td id="tbl-border-left"></td>
		<td><!--  start content-table-inner ...................................................................... START -->
		<div id="content-table-inner"><!--  start table-content  -->
		<div id="table-content_question"><?php
		$connexion=sql_connecter();

		if(isset($_GET['lang'])) // Si la variable $_GET['page'] existe...
		{
			$requete ='SELECT * FROM questions  WHERE etat_question = 1 AND id_langage ='.$langage.' ORDER BY id_question  DESC LIMIT 0, 10';
		}
		else
		{
			$requete ='SELECT * FROM questions  WHERE etat_question = 1 ORDER BY id_question DESC LIMIT 0, 10';
		}

		$result = sql_executer($connexion,$requete);
		if ($count = sql_nombre_lignes($result) == 0)
		{
			printf("Désolé, cette section est vide !");
		}
		else
		{
			while ($donnees = mysql_fetch_array($result))
			{
				$user=$donnees['id_user'];
				$id_question=$donnees['id_question'];
				$contenu_question=$donnees['contenu_question'];
				$titre_question=$donnees['titre_question'];
				$date_question=$donnees['date_question'];

				$max = 300; // Le nombre maximal de caractères à afficher en page d'accueil
				if (strlen($contenu_question)>$max) {
					$new_contenu = substr($contenu_question, 0, $max)."...";
				}
				else{
					$new_contenu = $contenu_question;
				}


				printf("<div id=\"cat_avatar\">");


				$requete3 ="SELECT path_avatar FROM avatars, users, questions WHERE avatars.id_avatar=users.id_avatar ";
				$requete3 .=" AND users.id_user=".$user;
				$result3 = sql_executer($connexion,$requete3);
				$donnees3=mysql_fetch_array($result3);
				$avatar=$donnees3['path_avatar'];

				$requete4 ="SELECT pseudo_user FROM users WHERE id_user =".$user ;
				$result4 = sql_executer($connexion,$requete4);
				$donnees4=mysql_fetch_array($result4);
				$pseudo_user=$donnees4['pseudo_user'];


				printf("<center>$pseudo_user</center><br />");

				printf("<img src=\"$avatar \">");
				printf("<p><center>Message : 4</center></p></div>");
				printf("<div id=\"cadre_question\">");
				printf("<span class=\"titre_question\">");
				printf("<h4>");
				printf("<a href=\"link/lire_question.php\">Qui sait ".htmlspecialchars($titre_question)."? </a>");
				printf("</h4> ");
				printf("</span> ");
				printf("<span class=\"date_create\">");
				printf("<p id=\"\" class=\"post-meta\">");
				printf("<em>le $date_question<br /></em>");
				printf("</p>");
				printf("</span>");
				printf("<div class=\"texte\">");
				printf("<br />");
				printf("<p>");
				printf(htmlspecialchars($new_contenu));
				printf("<br /><br /><span class=\"tags\">");
				$requete5 ="SELECT libelle_tag FROM tags, questions_tags WHERE tags.id_tag=questions_tags.id_tag ";
				$requete5 .=" AND questions_tags.id_question =".$id_question ;
				//die($requete5);
				$result5 = sql_executer($connexion,$requete5);
				while($donnees5=mysql_fetch_array($result5))
				{
					$libelle_tag=$donnees5['libelle_tag'];
					printf("<a href=\"\">$libelle_tag</a>");
				}
				printf("</span>\n");
				printf("<br /<br /><br /><a href=\"$page_lire_question&idq=$id_question\">Lire la suite  &rarr;</a>\n");
				printf("</p>");
				printf("</div>");
				printf("</div><br /><br />");

			}
		}
		?></div>
		<!--  end table-content  -->

		<div class="clear"></div>

		</div>
		<!--  end content-table-inner ............................................END  -->
		</td>
		<td id="tbl-border-right"></td>
	</tr>
	<tr>
		<th class="sized bottomleft"></th>
		<td id="tbl-border-bottom">&nbsp;</td>
		<th class="sized bottomright"></th>
	</tr>
</table>
<div class="clear">&nbsp;</div>
		<?php
		printf("<br /><p align=\"center\">Page : "); //Pour l'affichage, on centre la liste des pages
		for($i=1; $i<=$nombreDePages; $i++) //On fait notre boucle
		{
			//On va faire notre condition
			if($i==$pageActuelle) //Si il s'agit de la page actuelle...
			{
				printf("<span class=\"pagination1\">$i</span>");
			}
			else //Sinon...
			{
				printf("<span class=\"pagination2\"><a href=\"index.php?join=tutoriel&page=$i\">$i</a></span>");
			}
		}
		printf("</p>");
